package com.codedancer.campus.client.api.email;

import cn.hutool.core.bean.BeanUtil;
import com.codedancer.campus.client.api.email.ro.SendEmailRo;
import com.codedancer.campus.client.service.IEmailService;
import com.codedancer.campus.client.service.bo.email.SendEmailBo;
import com.codedancer.campus.common.anno.Limit;
import com.codedancer.campus.common.anno.PassToken;
import com.codedancer.campus.common.core.BaseApi;
import com.codedancer.campus.common.response.ApiResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/email")
@CrossOrigin(methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE,
    RequestMethod.PUT}, maxAge = 3600)
@Api(value = "邮箱Api", description = "邮箱Api", tags = "邮箱Api")
public class EmailApi extends BaseApi {

    @Autowired
    private IEmailService emailService;

    @PostMapping("/send-code")
    @ApiOperation("发送验证码")
    @PassToken
    @Limit(key = "EMAIL_CODE", permitsPerSecond = 5, timeout = 1000, msg = "系统繁忙，请稍后再试！")
        public ApiResponse<Void>sendCode(
        @RequestBody @Valid @ApiParam(value = "邮箱发送Ro", required = true)
            SendEmailRo sendEmailRo) {
        SendEmailBo sendEmailBo = BeanUtil.copyProperties(sendEmailRo, SendEmailBo.class);
        emailService.sendCode(sendEmailBo);
        return success();
    }
}
